<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$ui = new UI();
$tpl = new Template2();
$conf = new Config();
global $curruser;
if ($curruser['acl'] == 5 && $curruser['percent_sw'] == 1)
{
    err("你是固定占成的大股东，不能走飞！");
}
include( "class/class.Bnowconfig.php" );
$Bnowconfig = new Bnowconfig(0, $curruser['id']);
include( "member/news.php" );
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$userlist = GetUserList($curruser['id']);
if ($userlist == "")
{
    $ui->showErrorPage("<p>还没有建立会员！</p>");
    exit();
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>目前没有可以下注期数！</p>");
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$opnumber = $rs->f['n1'];
$rs->close();
$startmode = "";
$stopbet = $conf->get("stopbet");
if (date("Y-m-d") < $open)
{
    $startmode = "disabled";
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi") && 0 < $opnumber)
    {
        $startmode = "disabled";
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        $startmode = "disabled";
    }
}
$t1 = strtotime($open . " " . $stopbet);
$dt = date("Y-m-d");
$tpl->Set("timestap", $t1 - systime());
$tpl->set("phptime", systime());
$tpl->set("open", $open);
$tpl->set("round", d3($round));
$tpl->set("dt", $dt);
$tpl->set("lotto_id", $lotto_id);
$total_win = array();
$ary = array();
$ary[1] = array();
$ary[2] = array();
$i = 1;
while ($i < 12)
{
    $ary[1][$i] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
    $ary[2][$i] = array("rate" => 0, "amount" => 0, "count" => 0, "lost" => 0);
    $total_win[$i] = 0;
    ++$i;
}
$sql = "select * from bet where lotto_id=" . $lotto_id . " and bettype=19";
$userid = $curruser['id'];
$theacl = $curruser['acl'];
$userlist = GetUserList($userid);
if ($userlist == "")
{
    exit("Not Users!");
    exit();
}
$sql .= " and user_id in (" . $userlist . ") and rame in (0,9)";
$rs = getdb($sql);
while (!$rs->eof)
{
    $percent = 1;
    $sumback = 0;
    if ($theacl == 2)
    {
        $rate_s = $rs->f['rate1'] + $rs->f['rate2'];
        $sumback = $rs->f['back1'];
        $percent = $rs->f['mpercent'];
    }
    else if ($theacl == 3)
    {
        $rate_s = $rs->f['rate1'] + $rs->f['rate2'];
        $sumback = $rs->f['back2'];
        $percent = $rs->f['percent2'];
    }
    else if ($theacl == 4)
    {
        $rate_s = $rs->f['rate1'] + $rs->f['rate2'] + $rs->f['rate3'];
        $sumback = $rs->f['back3'];
        $percent = $rs->f['percent3'];
    }
    else
    {
        $rate_s = $rs->f['rate1'] + $rs->f['rate2'] + $rs->f['rate3'] + $rs->f['rate4'] + $rs->f['rate5'];
        $sumback = $rs->f['back4'];
        $percent = $rs->f['percent4'];
    }
    if (0 < $percent)
    {
        $percent /= 100;
    }
    $tmp = $percent * $rs->f['amount'];
    $num1 = intval(substr($rs->f['numbers'], 0, 1));
    $num2 = intval(substr($rs->f['numbers'], 2, 2));
    $ary[$num1][$num2]['amount'] += $tmp;
    ++$ary[$num1][$num2]['count'];
    $ary[$num1][$num2]['lost'] += $tmp * ( $rs->f['rate'] + $rate_s );
    $total_win[$num2] += $tmp * ( $sumback / 100 );
    $rs->movenext();
}
$rs->close();
if (2 < $theacl)
{
    $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and uperid" . $theacl . "=" . $userid . " and bettype=19 and rame in (0,9)";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $percent = 1;
        $backs = 100;
        if ($theacl == 255)
        {
            $backs = $rs->f['back5'];
            $percent = $rs->f['percent255'] - 0;
        }
        else if ($theacl == 5)
        {
            $backs = $rs->f['back4'];
            $percent = $rs->f['percent5'];
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back3'];
            $percent = $rs->f['percent4'];
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back2'];
            $percent = $rs->f['percent3'];
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
        $tmp = $percent * $rs->f['amount'];
        $num1 = intval(substr($rs->f['numbers'], 0, 1));
        $num2 = intval(substr($rs->f['numbers'], 2, 2));
        $ary[$num1][$num2]['amount'] += $tmp;
        ++$ary[$num1][$num2]['count'];
        $ary[$num1][$num2]['lost'] += $tmp * $rs->f['rate'];
        $total_win[$num2] += $tmp * ( $backs / 100 );
        $rs->movenext();
    }
    $rs->close();
}
$sql = "select * from bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and bettype=19 and rame in (0,9)";
$rs = getdb($sql);
while (!$rs->eof)
{
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back5'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back4'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back3'];
    }
    else
    {
        $backs = $rs->f['back2'];
    }
    $tmp = $rs->f['amount'];
    $num1 = intval(substr($rs->f['numbers'], 0, 1));
    $num2 = intval(substr($rs->f['numbers'], 2, 2));
    $ary[$num1][$num2]['amount'] -= $tmp;
    $ary[$num1][$num2]['lost'] -= $tmp * $rs->f['rate'];
    $total_win[$num2] -= $tmp * ( $backs / 100 );
    $rs->movenext();
}
$rs->close();
$rs = getdb("select * from rate1a where lotto_id=" . $lotto_id);
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$rs1 = get_srates_data($userid, $lotto_id, 1);
$i = 1;
while ($i < 12)
{
    if ($theacl == 5 || $rs1 == false)
    {
        $tpl->set("r19_1_" . d2($i), $rs->f["r19_1_" . d2($i)] - 0);
        $tpl->set("r19_2_" . d2($i), $rs->f["r19_2_" . d2($i)] - 0);
    }
    else
    {
        $tpl->set("r19_1_" . d2($i), $rs->f["r19_1_" . d2($i)] - $rs1->f["r19_1_" . d2($i)] - 0);
        $tpl->set("r19_2_" . d2($i), $rs->f["r19_2_" . d2($i)] - $rs1->f["r19_2_" . d2($i)] - 0);
    }
    $tpl->set("money1" . d2($i), $ary[1][$i]['amount']);
    $tpl->set("win1" . d2($i), show_numbercolor($total_win[$i] - $ary[1][$i]['lost']));
    $tpl->set("money2" . d2($i), $ary[2][$i]['amount']);
    $tpl->set("win2" . d2($i), show_numbercolor($total_win[$i] - $ary[2][$i]['lost']));
    ++$i;
}
$i = 1;
while ($i <= 12)
{
    $tpl->set("num" . $i, implode(", ", array_values($g_chinayearnumpic6[$i])));
    ++$i;
}
$tpl->set("b43", "");
$tpl->set("startnowdisable", $startmode);
include_once( "class_6h/class.menu.php" );
$fieldArr = array(
    "open" => $open,
    "lotto_id" => $lotto_id,
    "split_id" => 1
);
$onlineMenu = new menu_6h(12, $fieldArr);
$selectid = getvar("select") == "" ? 0 : getvar("select");
$menuList = $onlineMenu->getMenu($selectid);
$tpl->set("menuList", $menuList);
unset($onlineMenu);
$tpl->load("main", "bnow4.htm");
$tpl->set("jslang", $g_conf['language']);
$tpl->show("main");
$ui->footer();